package com.lanhang.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanhang.controller.vo.SpecificationVO;
import com.lanhang.entity.Specification;
import com.lanhang.entity.SpecificationExample;
import com.lanhang.entity.Specificationoption;
import com.lanhang.mapper.SpecificationMapper;
import com.lanhang.mapper.SpecificationoptionMapper;
import com.lanhang.service.SpecificationService;
import com.lanhang.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


    @Service
    public class SpecificationServiceImpl implements SpecificationService {
        @Autowired
        private SpecificationMapper specificationMapper;
        @Autowired
        private SpecificationoptionMapper specificationoptionMapper;

        @Override
        public List<Specification> findAll() {
            List<Specification> specifications = specificationMapper.selectByExample(null);
            /*specifications.forEach(sp -> {
                Specificationoption specificationoption = specificationoptionMapper.selectByPrimaryKey(sp.get());
                st.setCname(classes.getCname());
            });*/
            return specifications;
        }

        // 分页查询
        @Override
        public PageResult<Specification> findByPage(Integer page, Integer pageSize) {
            //1. 开始分页
            PageHelper.startPage(page,pageSize);
            // 2. 开始查询数据
            List<Specification> students = this.findAll();
            // 3. 将查询得到的数据转换为Page<Specification>对象
            Page<Specification> studentPage = (Page<Specification>) students;
            // 4. 转换为PageResult对象
            return new PageResult<>(studentPage.getResult(),studentPage.getTotal());

        }
        // 条件查询带分页功能
        @Override
        public PageResult<Specification> search(Integer page, Integer pageSize, SpecificationVO specificationVo) {
            // 1. 开始分页
            PageHelper.startPage(page, pageSize);
            // 2. 查询数据
            // 2.1 定义一次查询的实例
            SpecificationExample example = new SpecificationExample();
            // 2.2 添加查询条件到查询实例中
            SpecificationExample.Criteria criteria = example.createCriteria();
            if(StrUtil.isNotBlank(specificationVo.getSid())){
                criteria.andSidLike("%" + specificationVo.getSid() + "%");
            }
            if(StrUtil.isNotBlank(specificationVo.getSname())){
                criteria.Sname("%" + specificationVo.getSname() + "%");
            }

            List<Specification> specifications = specificationMapper.selectByExample(example);


            // 2.4 将上面的students对象转换为List集合
            Page<Specification> specificationPage = (Page<Specification>) specifications;
            return new PageResult<>(specificationPage.getResult(),specificationPage.getTotal());

        }


        // 3. 添加规格
        @Override
        public boolean save(Specification specification) {
            return specificationMapper.insert(specification) > 0;
        }
    }
